EasyExcel导出图片到Excel

您所在的位置:网站首页 easy excel官方文档 EasyExcel导出图片到Excel

EasyExcel导出图片到Excel

2023-06-11 08:09| 来源: 网络整理| 查看: 265

EasyExcel导出图片到Excel,需要自己重新写转换器,但是1000张以下导出是没问题的,10000以上会卡顿,有意向的小伙伴可以一起讨论下,如何处理大数量的图片导出到excel;

import com.alibaba.excel.converters.Converter; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.IoUtils; import org.apache.commons.lang3.StringUtils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Objects; public class MyStringImageConverter implements Converter { @Override public Class supportJavaTypeKey() { return String.class; } // 图片失效处理 @Override public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty,lobalConfiguration globalConfiguration) throws IOException { InputStream inputStream = null; try { if(StringUtils.isEmpty(value)|| Objects.isNull(value)) { return new WriteCellData("下载图片为空"); } URL urlValue = new URL(value); // 开启连接 URLConnection uc = urlValue.openConnection(); // 获取响应状态 int statusCode = ((HttpURLConnection)uc).getResponseCode(); switch (statusCode) { case 200: inputStream = urlValue.openStream(); break; default: return new WriteCellData("无法加载图片"); } byte[] bytes = IoUtils.toByteArray(inputStream); return new WriteCellData(bytes); } catch (ConnectException exception) { return new WriteCellData("无法加载图片"); }catch (FileNotFoundException fileNotFoundException) { return new WriteCellData("无法加载图片"); } finally { if (inputStream != null) { inputStream.close(); } } } }

运用示例:

@ExcelProperty(value = “XX图像”,index = 16,converter = MyStringImageConverter.class) @ColumnWidth(50) @ApiModelProperty(value = “图像URL地址”) private String pictureUrl;



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3